package customer.zspt.cn.customer.components.chat;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

import customer.zspt.cn.customer.Adapter.HistoryChatMsgAdapter;
import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.bean.ChatMsgDao;
import customer.zspt.cn.customer.sqlites.dao.chatMsgSqliteDao;
import customer.zspt.cn.customer.utils.TimestampToBj;

/**
 * Created by Administrator on 2018/8/23 0023.
 */

public class ChatHistory extends Activity implements View.OnClickListener{
    @ViewInject(R.id.history_ls)
    private ListView history_ls;
    @ViewInject(R.id.back)
    private ImageView back;
    @ViewInject(R.id.clock)
    private ImageView clock;
    private DatePicker datePicker;
    private long time;
    private chatMsgSqliteDao dao;
    private String kefuId;
    private List<ChatMsgDao> msgDaos ;
    private HistoryChatMsgAdapter adapter = null;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            msgDaos = (List<ChatMsgDao>) msg.obj;
            if(adapter==null){
                adapter = new HistoryChatMsgAdapter(msgDaos, ChatHistory.this);
                history_ls.setAdapter(adapter);
            }else{
                adapter.notifyDataSetChanged();
            }
        }
    };
    private int month;
    private int year;
    private int dayOfMonth;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_history_layout);
        ViewUtils.inject(this);
        kefuId = getIntent().getStringExtra("kefuId");
        dao= chatMsgSqliteDao.getInstance(this);
        initDate();
        initListener();
        showChooseDateDialog();
    }
    private void initDate() {
        String time = TimestampToBj.getTime(System.currentTimeMillis());
        String[] split = time.split("-");
        year = Integer.parseInt(split[0]);
        month = Integer.parseInt(split[1]);
        dayOfMonth = Integer.parseInt(split[2]);
    }

    private void initListener() {
        clock.setOnClickListener(this);
        back.setOnClickListener(this);
    }
    /**弹出日期选择框*/
    private void showChooseDateDialog() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        View view = View.inflate(this,
                R.layout.chat_order_content_dateinterval_datepicker,null);
        datePicker = view.findViewById(R.id.date_picker);
        datePicker.setMaxDate(System.currentTimeMillis());

        datePicker.init(year,month,dayOfMonth,null);
        dialog.setView(view);
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                 month = datePicker.getMonth();
                 year = datePicker.getYear();
                 dayOfMonth = datePicker.getDayOfMonth();
                getNowChat(month,year,dayOfMonth);
            }
        });
        dialog.setNegativeButton("取消",null);
        dialog.show();
    }
    private void getNowChat(int month, int year, int dayOfMonth) {
        month += 1;
        final String parse = year+"-"+(month<10?("0"+month):month)+"-"+(dayOfMonth<10?0+(dayOfMonth):(dayOfMonth));
        if(kefuId!=null){
            new Thread(){
                @Override
                public void run() {
                    List<ChatMsgDao> ChatMsgDao = dao.queryOnlyOneData(kefuId, parse);
                    if(ChatMsgDao==null)return;
                    Message message = new Message();
                    message.obj = ChatMsgDao;
                    mHandler.sendMessage(message);
                }
            }.start();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.clock:
                showChooseDateDialog();
                break;
            case R.id.back:
                finish();
                break;
        }
    }
}
